package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

//@CrossOrigin
@Tag(name = "轮播图接口")
@RestController
@RequestMapping("/api/banners")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary = "查询所有的轮播图")
    @GetMapping("/list")
    public Result<List<Banner>> list(){
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bannerLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list);
    }

    @Operation(summary = "查询激活状态的轮播图")
    @GetMapping("/active")
    public Result activeList(){
        List<Banner> list = bannerService.list(new LambdaQueryWrapper<Banner>().eq(Banner::getIsActive, true));
        return Result.success(list);
    }

    @Operation(summary = "根据id查询轮播图")
    @GetMapping("/{id}")
    public Result<Banner> getById(@PathVariable Long id){
        Banner banner = bannerService.getById(id);
        return Result.success(banner);
    }

//    @Operation(summary = "添加轮播图")
//    @PostMapping ("/add")
//    public Result add(@RequestBody Banner banner){
//        boolean save = bannerService.save(banner);
//        if (save){
//            return Result.success("添加成功");
//        }else {
//            return Result.error("添加失败");
//        }
//    }

//    @Operation(summary = "修改轮播图")
//    @PutMapping("/update")
//    public Result update(@RequestBody Banner banner){
//        boolean update = bannerService.updateById(banner);
//        if (update){
//            return Result.success("修改成功");
//        }else {
//            return Result.error("修改失败");
//        }
//    }

    @Operation(summary = "根据id删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        boolean delete = bannerService.removeById(id);
        if (delete){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @Operation(summary = "添加或更新轮播图信息")
    @RequestMapping(value = {"/save","/updata"},method = {RequestMethod.POST,RequestMethod.PUT})
    public Result save(@RequestBody Banner banner){
        boolean saveOrUpdate = bannerService.saveOrUpdate(banner);
        if (saveOrUpdate){
            return Result.success("保存成功");
        }else {
            return Result.error("保存失败");
        }
    }

    @Operation(summary = "上传轮播图")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file){
        //调用FileUploadService中的uploadFile方法上传图片
        String url = fileUploadService.uploadFile(file);
        return Result.success(url,"上传成功");
    }
}
